package com.ermax.aircond.report.web.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ermax.aircond.common.domain.UserDetail;
import com.ermax.aircond.common.domain.UserGroup;
import com.ermax.aircond.maintain.domain.Building;
import com.ermax.aircond.maintain.domain.Floor;
import com.ermax.aircond.maintain.domain.Machine;
import com.ermax.aircond.maintain.domain.Room;
import com.ermax.aircond.maintain.service.MaintainService;
import com.ermax.aircond.report.service.AuditLogService;
import com.ermax.aircond.resource.MessageBundle;
import com.ermax.aircond.util.domain.AuditedRevEntity;
import com.ermax.aircond.util.web.bean.DataTableBean;

@Scope("view")
@Component
public class MaintainAuditLogBean extends DataTableBean implements java.io.Serializable {

	/**
	 * 
	 */
	@Resource(name = "maintainService")
	private MaintainService maintainService;

	@Resource(name = "auditLogService")
	private AuditLogService auditLogService;

	private static final long serialVersionUID = 1L;
	private static final String STATUS_INIT = "INIT";
	private static final String STATUS_SEARCH = "SEARCH";

	private int searchType = 0;
	private int searchMethod = 0;
	private Date searchFromDate = new Date();
	private Date searchToDate = new Date();
	private long searchAuditUserId = 0L;
	private List<SelectItem> auditUsers = new ArrayList<SelectItem>();
	private List<SelectItem> searchOption = new ArrayList<SelectItem>();
	private String status = "STATUS_INIT";

	private List<AuditedRevEntity> auditEntities = new ArrayList<AuditedRevEntity>();

	@PostConstruct
	public void init() {
		try {
			reset();
		} catch (Exception ex) {
			String message = MessageBundle.getInstance().getMessage("error_init_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale());
			FacesMessage facesMessage = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		}
	}

	private void reset() throws Exception {
		searchType = 0;
		searchMethod = 0;
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -7);
		searchFromDate = cal.getTime();
		searchToDate = new Date();
		searchAuditUserId = 0L;
		searchOption.clear();
		auditUsers.clear();
		auditEntities.clear();
		status = STATUS_INIT;

		auditUsers.add(new SelectItem(0, "ALL"));
		List<UserDetail> userDetails = maintainService.getAllUserDetails();
		for (int i = 0; i < userDetails.size(); i++) {
			UserDetail userDetail = userDetails.get(i);
			auditUsers.add(new SelectItem(userDetail.getUserId(), userDetail.getUsername()));
		}
	}

	public void search() {
		Class<?> clazz = null;
		switch (searchType) {
		case 0:
			clazz = UserGroup.class;
			break;
		case 1:
			clazz = UserDetail.class;
			break;
		case 2:
			clazz = Building.class;
			break;
		case 3:
			clazz = Floor.class;
			break;
		case 4:
			clazz = Room.class;
			break;
		case 5:
			clazz = Machine.class;
			break;
		}

		auditEntities = auditLogService.getRevEntities(clazz, searchFromDate, searchToDate, searchAuditUserId);
		status = STATUS_SEARCH;
	}
	
	public void back(){
		status = STATUS_INIT;
	}

	public int getSearchType() {
		return searchType;
	}

	public void setSearchType(int searchType) {
		this.searchType = searchType;
	}

	public int getSearchMethod() {
		return searchMethod;
	}

	public void setSearchMethod(int searchMethod) {
		this.searchMethod = searchMethod;
	}

	public List<SelectItem> getSearchOption() {
		return searchOption;
	}

	public void setSearchOption(List<SelectItem> searchOption) {
		this.searchOption = searchOption;
	}

	

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getSearchFromDate() {
		return searchFromDate;
	}

	public void setSearchFromDate(Date searchFromDate) {
		this.searchFromDate = searchFromDate;
	}

	public Date getSearchToDate() {
		return searchToDate;
	}

	public void setSearchToDate(Date searchToDate) {
		this.searchToDate = searchToDate;
	}

	public List<SelectItem> getAuditUsers() {
		return auditUsers;
	}

	public void setAuditUsers(List<SelectItem> auditUsers) {
		this.auditUsers = auditUsers;
	}

	public long getSearchAuditUserId() {
		return searchAuditUserId;
	}

	public void setSearchAuditUserId(long searchAuditUserId) {
		this.searchAuditUserId = searchAuditUserId;
	}

	public List<AuditedRevEntity> getAuditEntities() {
		return auditEntities;
	}

	public void setAuditEntities(List<AuditedRevEntity> auditEntities) {
		this.auditEntities = auditEntities;
	}

}
